/*
 * TabelaNotas.java
 *
 * Created on 23 de Novembro de 2007, 01:47
 */

package gui;

import javax.swing.JFrame;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import dados.Aluno;
import dados.BancoQuestoes;
import dados.PastaCorrecao;

/**
 *
 * @author  UltraXP
 */
public class TabelaNotas extends javax.swing.JPanel {
    
    JFrame pai = null;

    /** Creates new form TabelaNotas */
    public TabelaNotas(JFrame pai) {
    	System.out.println("gui.TabelaNotas");
        initComponents();
        this.pai = pai;
        preencherTabela();

    }
    
    private void preencherTabela() {
        Aluno[] alunos = PastaCorrecao.getInstancia().getAlunos();
        tabela.setModel(new TableModel() {
            public void addTableModelListener(TableModelListener l) {
            }
            
            public Class<?> getColumnClass(int columnIndex) {
                return "".getClass();
            }
            
            public int getColumnCount() {
                return BancoQuestoes.getInstancia()
                        .getTamanhoBanco()+2;
            }
            
            public String getColumnName(int columnIndex) {
               int nroQuestoes = BancoQuestoes.getInstancia()
                       .getTamanhoBanco();
                if (columnIndex == 0) {
                    return "Aluno";
                } else if (columnIndex == nroQuestoes+1) {
                    return "Nota Final";
                } else {
                    return "Questao "+ columnIndex;
                }
            }
            
            public int getRowCount() {
                return PastaCorrecao.getInstancia().getAlunos().length;
            }
            
            public Object getValueAt(int rowIndex, int columnIndex) {
                int nroQuestoes = BancoQuestoes.getInstancia().getTamanhoBanco();
                if (columnIndex == 0) {
                    return PastaCorrecao.getInstancia().getAlunos()[rowIndex].getDiretorioAluno().getName();
                } else if (columnIndex == nroQuestoes+1) {
                    return PastaCorrecao.getInstancia().getAlunos()[rowIndex].getNotaFinal();
                }else {
                    try{
                        return PastaCorrecao.getInstancia().getAlunos()[rowIndex].getNotaQuestao(columnIndex -1)*nroQuestoes;
                    }catch(IndexOutOfBoundsException e){
                        return 0;
                    }
                }
            }
            
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
            
            public void removeTableModelListener(TableModelListener l) {  }
            
            public void setValueAt(Object aValue, int rowIndex, int columnIndex) {  }
            
        });
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" C�digo Gerado ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Título 1", "Título 2", "Título 3", "Título 4"
            }
        ));
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMouseClicked(evt);
            }
        });

        jScrollPane1.setViewportView(tabela);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 411, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 288, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMouseClicked
        new Relatorio(tabela.getSelectedRow(), "Relatório do aluno " + (String)tabela.getValueAt(tabela.getSelectedRow(), 0), pai).setVisible(true);        
    }//GEN-LAST:event_tabelaMouseClicked
    
    
    // Declara��o de vari�veis - n�o modifique//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabela;
    // Fim da declara��o de vari�veis//GEN-END:variables
    
}
